package com.shixun.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.shixun.dao.BaseDao;
import com.shixun.entity.FileEntity;
import com.shixun.entity.FpinglunEntity;
import com.shixun.entity.UserEntity;
import com.shixun.service.FcbService;
import com.shixun.service.ZhyService;
import com.sun.org.apache.commons.digester.xmlrules.FromXmlRuleSet;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.struts2.components.Else;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.SourceExtractor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by fan-computer on 2016/12/12.
 */
@Controller
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/TestSX")
public class FcbAction extends ActionSupport {
    @Autowired
    private UserEntity userEntity;
    @Autowired
    private FileEntity fileEntity;
    @Autowired
    private FpinglunEntity fpinglunEntity;
    @Autowired
    private FcbService fcbService;
    @Autowired
    private ZhyService zhyService;

    /**
     * 注册
     *
     * @return
     */
    @Action(value = "userRegister", results = {
            @Result(location = "/index.jsp"),
            @Result(name = "error", location = "/index.jsp")
    })
    public String userRegister() {
        System.out.println("11111");
        if (fcbService.userregisterService(userEntity)) {
            ActionContext.getContext().put("fubtishi", "注册成功");
            return SUCCESS;
        } else {
            ActionContext.getContext().put("fubtishi", "注册失败,要求登录号唯一");
            return ERROR;
        }
    }

    /**
     * 到登录页面
     */
    @Action(value = "loginSuccess", results = {
            @Result(location = "/shixun/indexLogin", type = "redirect"),
            @Result(name = "login", location = "/adminLogin/indexLogin.jsp"),  //登录到管理员页面
            @Result(name = "error", location = "/fristpage.jsp")
    })
    public String loginSuccess() {
        UserEntity uList = (UserEntity) BaseDao.getSessions().get("user");
        if ("1".equals(uList.getState())) {
            return SUCCESS;
        } else if ("admin".equals(uList.getState())) {
            ActionContext.getContext().put("adminFristLogin", "管理员第一次登陆");
            return LOGIN;
        } else if ("0".equals(uList.getState())) {
            ActionContext.getContext().put("fubtishi", "此用户已经被注销");
            return ERROR;
        }
        return null;
    }

    /**
     * 登录验证
     */
    @Action(value = "userLogin", results = {
            @Result(location = "loginSuccess", type = "redirect"),
            @Result(name = "error", location = "/index.jsp")
    })
    public String userLogin() {
        System.out.println("111111111::" + userEntity.getLoginId());
        if (fcbService.userloginService(userEntity)) {

            return SUCCESS;
        } else {
            System.out.println("#############################");
            ActionContext.getContext().put("fubtishi", "登陆失败，用户名或密码错误");
            return ERROR;
        }
    }

    /**
     * 注销登录
     */
    @Action(value = "loginOut", results = {
            @Result(location = "/index.jsp")
    })
    public String loginOut() {
        Map session = ActionContext.getContext().getSession();
        session.clear();
        return SUCCESS;
    }

    /**
     * 发表帖子
     *
     * @return
     */
    @Action(value = "TieziAction", results = {
            @Result(location = "selectFourInfo", type = "redirect"),
            @Result(name = "error", location = "/userLogin/postTiezi.jsp")
    })
    public String TieziAction() throws Exception {
        if (fcbService.saveFileService(fileEntity)) {
            return SUCCESS;
        }
        return ERROR;
    }


    /**
     * 查找四条帖子的信息
     */
    @Action(value = "selectFourInfo", results = {
            @Result(location = "/userLogin/community.jsp"),
            @Result(name = "error", location = "/userLogin/community.jsp")
    })
    public String selectFourInfo() {
        List<FileEntity> list = fcbService.searchAllFileService();
        List<FileEntity> fourList = new ArrayList<>();
        if (list.size() > 0) {
            if (list.size() > 6) {
                for (int i = 0; i < list.size(); i++) {
                    fourList.add(list.get(i));
                    if (fourList.size() == 6) {
                        break;
                    }
                }
                BaseDao.getRequest().setAttribute("searchSuccess", fourList);
                ActionContext.getContext().put("searchOneInfo", list.get(list.size() - 1));
                return SUCCESS;
            } else {
                BaseDao.getRequest().setAttribute("searchSuccess", list);
                ActionContext.getContext().put("searchOneInfo", list.get(list.size() - 1));
                return SUCCESS;
            }
        } else {
            ActionContext.getContext().put("searchFaile", "您还没有发表任何文章");
            return ERROR;
        }
    }

    /**
     * 得到发表的帖子
     *
     * @return
     */
    @Action(value = "getTieziAction", results = {
            @Result(location = "/userLogin/community.jsp"),
            @Result(name = "error", location = "/userLogin/community.jsp")
    })
    public String getTieziAction() {
        List<FileEntity> list = fcbService.getTiezi();
        if (list.size() > 0) {
            BaseDao.getRequest().setAttribute("searchOneInfo", list);
            return SUCCESS;
        } else {
            ActionContext.getContext().put("getTieziFail", "还未发表帖子");
            return ERROR;
        }
    }

    /**
     * 查找所有帖子的信息
     */
    @Action(value = "searchAllFile", results = {
            @Result(location = "/userLogin/community.jsp"),
            @Result(name = "error", location = "userLogin/community.jsp")
    })
    public String searchAllFile() {
        List<FileEntity> allFile = fcbService.searchAllFileService();
        if (allFile.size() > 0) {
            ActionContext.getContext().put("hiddenMore", "展示更多的帖子信息");
            BaseDao.getRequest().setAttribute("searchSuccess", allFile);
            ActionContext.getContext().put("searchOneInfo", allFile.get(allFile.size() - 1));
            return SUCCESS;
        } else {
            ActionContext.getContext().put("errorMore", "沒有更多的帖子信息");
            return ERROR;
        }
    }

    /**
     * 通过id获得每一条帖子的详情
     *
     * @return
     */
    @Action(value = "searchOneTiezi", results = {
            @Result(location = "/userLogin/tiezi_details.jsp"),
            @Result(name = "error", location = "/userLogin/community.jsp")
    })
    public String searchOneTiezi() {
        FileEntity fileEntity = fcbService.searchOneTieziService(BaseDao.getRequest().getParameter("Tieziid"));
        List<FpinglunEntity> flist = fcbService.getAllPinglun(BaseDao.getRequest().getParameter("Tieziid"));
        if (fileEntity == null) {
            ActionContext.getContext().put("searchTieziError", "未查找到发表的帖子，请稍后重试！");
            return ERROR;
        } else {
            if (flist.size() > 0) {
                BaseDao.getRequest().setAttribute("getPinglun", flist);
            }
            fileEntity.setFtime(fileEntity.getFtime());
            ActionContext.getContext().put("searchTieziSuccess", fileEntity);
            return SUCCESS;
        }
    }


    /**
     * 点赞，将点赞数据存储到数据库
     *
     * @return
     */
    @Action(value = "dianzan")
    public void dianzan() {
        fcbService.saveDianzan(BaseDao.getRequest().getParameter("dianZan"));
    }


    /**
     * 发表评论
     *
     * @return
     */
    @Action(value = "tiezaiPinglun")
    public void tiezaiPinglun() {
        fcbService.PostPinglun(BaseDao.getRequest().getParameter("tieziid"), BaseDao.getRequest().getParameter("pinglunContent"));
    }


    /**
     * 通过id获得每一条帖子的详情
     *
     * @return
     */
    @Action(value = "searchOneTieziMyself", results = {
            @Result(location = "/userLogin/tiezi_details.jsp"),
            @Result(name = "error", location = "/userLogin/myself.jsp")
    })
    public String searchOneTieziMyself() {
        FileEntity fileEntity = fcbService.searchOneTieziService(BaseDao.getRequest().getParameter("tiziId"));
        if (fileEntity == null) {
            return ERROR;
        } else {
            System.out.println("发帖子啦啦啦");
            fileEntity.setFtime(fileEntity.getFtime());
            ActionContext.getContext().put("searchTieziSuccess", fileEntity);
            return SUCCESS;
        }
    }

    /**
     * 未登录时得到发表的帖子
     *
     * @return
     */
    @Action(value = "getuserTieziAction", results = {
            @Result(location = "/user/shequ.jsp"),
            @Result(name = "error", location = "/user/shequ.jsp")
    })
    public String getuserTieziAction() {
        List<FileEntity> list = fcbService.getTiezi();
        System.out.println("############未登录时获得的帖子" + list.size());
        if (list.size() > 0) {
            BaseDao.getRequest().setAttribute("searchSuccess", list);
            return SUCCESS;
        } else {
            ActionContext.getContext().put("searchFaile", "还未发表帖子");
            return ERROR;
        }
    }

    /**
     * 未登录前通过id获得每一条帖子的详情
     *
     * @return
     */
    @Action(value = "searchOneuserTiezi", results = {
            @Result(location = "/user/shequ_details.jsp"),
            @Result(name = "error", location = "/user/shequ.jsp")
    })
    public String searchOneuserTiezi() {
        FileEntity fileEntity = fcbService.searchOneTieziService(BaseDao.getRequest().getParameter("Tieziid"));
        List<FpinglunEntity> flist = fcbService.getAllPinglun(BaseDao.getRequest().getParameter("Tieziid"));
        if (fileEntity == null) {
            ActionContext.getContext().put("searchTieziError", "未查找到发表的帖子，请稍后重试！");
            return ERROR;
        } else {
            if (flist.size() > 0) {
                BaseDao.getRequest().setAttribute("getPinglun", flist);
            }
            fileEntity.setFtime(fileEntity.getFtime());
            ActionContext.getContext().put("searchTieziSuccess", fileEntity);
            return SUCCESS;
        }
    }


    /**
     * 后台管理中心通过ID查看一条文章
     */
    @Action(value = "findArticleAction", results = {
            @Result(location = "/adminLogin/viewArticles.jsp"),
            @Result(name = "error", location = "/adminLogin/articleManager.jsp")
    })
    public String findArticleAction() {
        FileEntity fileEntity = fcbService.findArticle(BaseDao.getRequest().getParameter("fileid"));
        if (fileEntity == null) {
            ActionContext.getContext().put("ErrorArticle", "查看文章失败，请稍后再试！");
            return ERROR;
        } else {
            ActionContext.getContext().put("SuccessArticle", fileEntity);
            return SUCCESS;
        }
    }


    /**
     * 后台管理删除文章
     *
     * @return
     */
    @Action(value = "deleteArticleAction", results = {
            @Result(location = "/shixun/allFile",type = "redirect"),
            @Result(name = "error", location = "/userLogin/articleManager.jsp")
    })
    public String deleteArticleAction() {
        if (fcbService.deleteArticleService(BaseDao.getRequest().getParameter("fileid"))){
            return SUCCESS;
        }else {
            return ERROR;
        }
    }


    /**
     * 后台管理中心查看帖子
     * @return
     */
    @Action(value = "findTieziAction", results = {
            @Result(location = "/adminLogin/viewTiezi.jsp"),
            @Result(name = "error", location = "/adminLogin/postManager.jsp")
    })
    public String findTieziAction() {
        FileEntity fileEntity = fcbService.findTiezi(BaseDao.getRequest().getParameter("tieziid"));
        if (fileEntity == null) {
            ActionContext.getContext().put("ErrorTiezi", "查看文章失败，请稍后再试！");
            return ERROR;
        } else {
            ActionContext.getContext().put("SuccessTiezi", fileEntity);
            return SUCCESS;
        }
    }

    /**
     * 后台管理中心删除帖子
     * @return
     */
    @Action(value = "deleteTieziAction", results = {
            @Result(location = "/shixun/Tiezi",type = "redirect"),
            @Result(name = "error", location = "/userLogin/postManager.jsp")
    })
    public String deleteTieziAction() {
        if (fcbService.deleteTieziService(BaseDao.getRequest().getParameter("tieziid"))){
            return SUCCESS;
        }else {
            return ERROR;
        }
    }





    public UserEntity getUserEntity() {
        return userEntity;
    }

    public void setUserEntity(UserEntity userEntity) {
        this.userEntity = userEntity;
    }

    public FileEntity getFileEntity() {
        return fileEntity;
    }

    public void setFileEntity(FileEntity fileEntity) {
        this.fileEntity = fileEntity;
    }

    public FpinglunEntity getFpinglunEntity() {
        return fpinglunEntity;
    }

    public void setFpinglunEntity(FpinglunEntity fpinglunEntity) {
        this.fpinglunEntity = fpinglunEntity;
    }

    public FcbService getFcbService() {
        return fcbService;
    }

    public void setFcbService(FcbService fcbService) {
        this.fcbService = fcbService;
    }
}
